package cn.ibizlab.odoo.core.odoo_mrp.service.impl;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.math.BigInteger;
import java.util.Collection;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import cn.ibizlab.odoo.core.client.model.Imrp_workcenter_productivity;
import cn.ibizlab.odoo.core.odoo_mrp.domain.Mrp_workcenter_productivity;
import cn.ibizlab.odoo.core.odoo_mrp.filter.Mrp_workcenter_productivitySearchContext;
import cn.ibizlab.odoo.core.odoo_mrp.service.IMrp_workcenter_productivityService;
import cn.ibizlab.odoo.util.SearchResult;
import cn.ibizlab.odoo.util.log.IBIZLog;

/**
 * 实体[工作中心生产力日志] 服务对象接口实现
 */
@Slf4j
@IBIZLog
@Service
public class Mrp_workcenter_productivityServiceImpl implements IMrp_workcenter_productivityService {

    @Autowired
    private cn.ibizlab.odoo.core.client.service.Imrp_workcenter_productivityClientService mrp_workcenter_productivityClientService;

    @Override
    public boolean create(Mrp_workcenter_productivity mrp_workcenter_productivity) {
        boolean bOk = false ;
        Imrp_workcenter_productivity clientModel = this.convert2Model(mrp_workcenter_productivity,null);
        mrp_workcenter_productivityClientService.create(clientModel) ;
        this.convert2Domain(clientModel,mrp_workcenter_productivity);
        bOk = true ;

        return bOk ;
    }

    @Override
    public boolean update(Mrp_workcenter_productivity mrp_workcenter_productivity) {
        boolean bOk = false ;
        Imrp_workcenter_productivity clientModel = this.convert2Model(mrp_workcenter_productivity,null);
        mrp_workcenter_productivityClientService.update(clientModel) ;
        this.convert2Domain(clientModel,mrp_workcenter_productivity) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public void createBatch(List<Mrp_workcenter_productivity> mrp_workcenter_productivitys, int batchSize) {

                        //mrp_workcenter_productivityClientService.createBatch(mrp_workcenter_productivitys,batchSize) ;

    }

    @Override
    public Mrp_workcenter_productivity get(Integer id) {
        Imrp_workcenter_productivity clientModel = mrp_workcenter_productivityClientService.createModel();
        clientModel.setId(id);
        mrp_workcenter_productivityClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public Mrp_workcenter_productivity get(Mrp_workcenter_productivity mrp_workcenter_productivity) {
        Mrp_workcenter_productivity retmrp_workcenter_productivity = this.get(mrp_workcenter_productivity.getId()) ;
    	BeanCopier copier=BeanCopier.create(retmrp_workcenter_productivity.getClass(), mrp_workcenter_productivity.getClass(), false);
        copier.copy(retmrp_workcenter_productivity, mrp_workcenter_productivity, null);
        return mrp_workcenter_productivity ;
    }

    @Override
    public boolean remove(Integer id) {
        boolean bOk = false ;
        Imrp_workcenter_productivity clientModel = mrp_workcenter_productivityClientService.createModel();
        clientModel.setId(id);
        mrp_workcenter_productivityClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public void removeBatch(Collection<? extends Serializable> idList, int batchSize) {

        //mrp_workcenter_productivityClientService.removeBatch(mrp_workcenter_productivitys) ;

    }

    @Override
    public void updateBatch(List<Mrp_workcenter_productivity> mrp_workcenter_productivitys, int batchSize) {

        //mrp_workcenter_productivityClientService.updateBatch(mrp_workcenter_productivitys,batchSize) ;

    }

    /**
     * 查询集合 默认查询
     */
    @Override
    public Page<Mrp_workcenter_productivity> searchDefault(Mrp_workcenter_productivitySearchContext context) {
        List<Mrp_workcenter_productivity> list = new ArrayList<Mrp_workcenter_productivity>();
        Page<cn.ibizlab.odoo.core.client.model.Imrp_workcenter_productivity> clientModelList = mrp_workcenter_productivityClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imrp_workcenter_productivity clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mrp_workcenter_productivity>(list, context.getPageable(), clientModelList.getTotalElements());
    }


    /**
     * 根据工作中心生产力损失查询
     */
    @Override
    public Page<Mrp_workcenter_productivity> selectByOdoo_loss(Integer id){
        Mrp_workcenter_productivitySearchContext context = new Mrp_workcenter_productivitySearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Mrp_workcenter_productivity> list = new ArrayList<Mrp_workcenter_productivity>();
        Page<cn.ibizlab.odoo.core.client.model.Imrp_workcenter_productivity> clientModelList = mrp_workcenter_productivityClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imrp_workcenter_productivity clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mrp_workcenter_productivity>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据工作中心查询
     */
    @Override
    public Page<Mrp_workcenter_productivity> selectByOdoo_workcenter(Integer id){
        Mrp_workcenter_productivitySearchContext context = new Mrp_workcenter_productivitySearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Mrp_workcenter_productivity> list = new ArrayList<Mrp_workcenter_productivity>();
        Page<cn.ibizlab.odoo.core.client.model.Imrp_workcenter_productivity> clientModelList = mrp_workcenter_productivityClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imrp_workcenter_productivity clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mrp_workcenter_productivity>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据工单查询
     */
    @Override
    public Page<Mrp_workcenter_productivity> selectByOdoo_workorder(Integer id){
        Mrp_workcenter_productivitySearchContext context = new Mrp_workcenter_productivitySearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Mrp_workcenter_productivity> list = new ArrayList<Mrp_workcenter_productivity>();
        Page<cn.ibizlab.odoo.core.client.model.Imrp_workcenter_productivity> clientModelList = mrp_workcenter_productivityClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imrp_workcenter_productivity clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mrp_workcenter_productivity>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Mrp_workcenter_productivity> selectByOdoo_create(Integer id){
        Mrp_workcenter_productivitySearchContext context = new Mrp_workcenter_productivitySearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Mrp_workcenter_productivity> list = new ArrayList<Mrp_workcenter_productivity>();
        Page<cn.ibizlab.odoo.core.client.model.Imrp_workcenter_productivity> clientModelList = mrp_workcenter_productivityClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imrp_workcenter_productivity clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mrp_workcenter_productivity>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Mrp_workcenter_productivity> selectByOdoo_user(Integer id){
        Mrp_workcenter_productivitySearchContext context = new Mrp_workcenter_productivitySearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Mrp_workcenter_productivity> list = new ArrayList<Mrp_workcenter_productivity>();
        Page<cn.ibizlab.odoo.core.client.model.Imrp_workcenter_productivity> clientModelList = mrp_workcenter_productivityClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imrp_workcenter_productivity clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mrp_workcenter_productivity>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Mrp_workcenter_productivity> selectByOdoo_write(Integer id){
        Mrp_workcenter_productivitySearchContext context = new Mrp_workcenter_productivitySearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Mrp_workcenter_productivity> list = new ArrayList<Mrp_workcenter_productivity>();
        Page<cn.ibizlab.odoo.core.client.model.Imrp_workcenter_productivity> clientModelList = mrp_workcenter_productivityClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imrp_workcenter_productivity clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mrp_workcenter_productivity>(list, context.getPageable(), clientModelList.getTotalElements());

    }



    public Imrp_workcenter_productivity convert2Model(Mrp_workcenter_productivity domain , Imrp_workcenter_productivity model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = mrp_workcenter_productivityClientService.createModel() ;
        }

         if(domain.getCreate_dateDirtyFlag())
            model.setCreate_date(domain.getCreate_date());
         if(domain.getIdDirtyFlag())
            model.setId(domain.getId());
         if(domain.getDescriptionDirtyFlag())
            model.setDescription(domain.getDescription());
         if(domain.getDisplay_nameDirtyFlag())
            model.setDisplay_name(domain.getDisplay_name());
         if(domain.getDurationDirtyFlag())
            model.setDuration(domain.getDuration());
         if(domain.getDate_endDirtyFlag())
            model.setDate_end(domain.getDate_end());
         if(domain.getWrite_dateDirtyFlag())
            model.setWrite_date(domain.getWrite_date());
         if(domain.getDate_startDirtyFlag())
            model.setDate_start(domain.getDate_start());
         if(domain.get__last_updateDirtyFlag())
            model.set__last_update(domain.get__last_update());
         if(domain.getCreate_uid_textDirtyFlag())
            model.setCreate_uid_text(domain.getCreate_uid_text());
         if(domain.getWorkcenter_id_textDirtyFlag())
            model.setWorkcenter_id_text(domain.getWorkcenter_id_text());
         if(domain.getUser_id_textDirtyFlag())
            model.setUser_id_text(domain.getUser_id_text());
         if(domain.getLoss_typeDirtyFlag())
            model.setLoss_type(domain.getLoss_type());
         if(domain.getWorkorder_id_textDirtyFlag())
            model.setWorkorder_id_text(domain.getWorkorder_id_text());
         if(domain.getLoss_id_textDirtyFlag())
            model.setLoss_id_text(domain.getLoss_id_text());
         if(domain.getProduction_idDirtyFlag())
            model.setProduction_id(domain.getProduction_id());
         if(domain.getWrite_uid_textDirtyFlag())
            model.setWrite_uid_text(domain.getWrite_uid_text());
         if(domain.getUser_idDirtyFlag())
            model.setUser_id(domain.getUser_id());
         if(domain.getWorkorder_idDirtyFlag())
            model.setWorkorder_id(domain.getWorkorder_id());
         if(domain.getWorkcenter_idDirtyFlag())
            model.setWorkcenter_id(domain.getWorkcenter_id());
         if(domain.getLoss_idDirtyFlag())
            model.setLoss_id(domain.getLoss_id());
         if(domain.getWrite_uidDirtyFlag())
            model.setWrite_uid(domain.getWrite_uid());
         if(domain.getCreate_uidDirtyFlag())
            model.setCreate_uid(domain.getCreate_uid());
        return model ;
    }

    public Mrp_workcenter_productivity convert2Domain( Imrp_workcenter_productivity model ,Mrp_workcenter_productivity domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new Mrp_workcenter_productivity();
        }

        if(model.getCreate_dateDirtyFlag())
            domain.setCreate_date(model.getCreate_date());
        if(model.getIdDirtyFlag())
            domain.setId(model.getId());
        if(model.getDescriptionDirtyFlag())
            domain.setDescription(model.getDescription());
        if(model.getDisplay_nameDirtyFlag())
            domain.setDisplay_name(model.getDisplay_name());
        if(model.getDurationDirtyFlag())
            domain.setDuration(model.getDuration());
        if(model.getDate_endDirtyFlag())
            domain.setDate_end(model.getDate_end());
        if(model.getWrite_dateDirtyFlag())
            domain.setWrite_date(model.getWrite_date());
        if(model.getDate_startDirtyFlag())
            domain.setDate_start(model.getDate_start());
        if(model.get__last_updateDirtyFlag())
            domain.set__last_update(model.get__last_update());
        if(model.getCreate_uid_textDirtyFlag())
            domain.setCreate_uid_text(model.getCreate_uid_text());
        if(model.getWorkcenter_id_textDirtyFlag())
            domain.setWorkcenter_id_text(model.getWorkcenter_id_text());
        if(model.getUser_id_textDirtyFlag())
            domain.setUser_id_text(model.getUser_id_text());
        if(model.getLoss_typeDirtyFlag())
            domain.setLoss_type(model.getLoss_type());
        if(model.getWorkorder_id_textDirtyFlag())
            domain.setWorkorder_id_text(model.getWorkorder_id_text());
        if(model.getLoss_id_textDirtyFlag())
            domain.setLoss_id_text(model.getLoss_id_text());
        if(model.getProduction_idDirtyFlag())
            domain.setProduction_id(model.getProduction_id());
        if(model.getWrite_uid_textDirtyFlag())
            domain.setWrite_uid_text(model.getWrite_uid_text());
        if(model.getUser_idDirtyFlag())
            domain.setUser_id(model.getUser_id());
        if(model.getWorkorder_idDirtyFlag())
            domain.setWorkorder_id(model.getWorkorder_id());
        if(model.getWorkcenter_idDirtyFlag())
            domain.setWorkcenter_id(model.getWorkcenter_id());
        if(model.getLoss_idDirtyFlag())
            domain.setLoss_id(model.getLoss_id());
        if(model.getWrite_uidDirtyFlag())
            domain.setWrite_uid(model.getWrite_uid());
        if(model.getCreate_uidDirtyFlag())
            domain.setCreate_uid(model.getCreate_uid());
        return domain ;
    }


}
